package com.sise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sise.bean.CodeMsg;
import com.sise.bean.OrderMasterBean;
import com.sise.entity.OrderMaster;
import com.sise.entity.StoreInfo;
import com.sise.mapper.OrderMasterMapper;
import com.sise.mapper.StoreInfoMapper;
import com.sise.service.IOrderMasterService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zj
 */
@Service
public class
OrderMasterServiceImpl extends ServiceImpl<OrderMasterMapper, OrderMaster> implements IOrderMasterService {

    @Resource
    private OrderMasterMapper orderMasterMapper;

    @Resource
    private StoreInfoMapper storeInfoMapper;

    @Override
    public List<OrderMasterBean> findAllOrder() {
        QueryWrapper<OrderMaster> orderMasterQueryWrapper = new QueryWrapper<>();
        orderMasterQueryWrapper.lambda().orderByDesc(OrderMaster::getCreateTime);
        List<OrderMaster> orderMasterList = orderMasterMapper.selectList(orderMasterQueryWrapper);
        return orderMasterBeanTypeConversionAll(orderMasterList);
    }

    @Override
    public CodeMsg orderStatusPaidCount() {
        QueryWrapper<OrderMaster> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(OrderMaster::getOrderStatus, 1);
        return new CodeMsg().setCode(200).setMsg("获取成功").setData(orderMasterMapper.selectCount(queryWrapper));
    }

    @Override
    public CodeMsg<String> deliveryAndCompletion(String[] orderIdList, Integer status) {
        QueryWrapper<OrderMaster> orderMasterQueryWrapper;
        for (String orderId : orderIdList) {
            orderMasterQueryWrapper = new QueryWrapper<>();
            orderMasterQueryWrapper.lambda().eq(OrderMaster::getOrderId, orderId);
            OrderMaster orderMaster = orderMasterMapper.selectOne(orderMasterQueryWrapper);
            if (status.equals(1)) {
                orderMaster.setOrderStatus(2);
            } else if (status.equals(2)) {
                orderMaster.setOrderStatus(3);
            } else {
                return new CodeMsg<String>().setCode(400).setMsg("方法出错");
            }
            orderMasterMapper.updateById(orderMaster);
        }
        return new CodeMsg<String>().setCode(200).setMsg("订单已完成");
    }

    /**
     * List<OrderMasterBean>类型转换
     */
    public List<OrderMasterBean> orderMasterBeanTypeConversionAll(List<OrderMaster> orderMasterList) {
        List<OrderMasterBean> orderMasterBeanList = new ArrayList<>();
        for (OrderMaster orderMaster : orderMasterList) {
            OrderMasterBean orderMasterBean = orderMasterBeanTypeConversion(orderMaster);
            orderMasterBeanList.add(orderMasterBean);
        }
        return orderMasterBeanList;
    }

    /**
     * 订单主表类型转换
     */
    public OrderMasterBean orderMasterBeanTypeConversion(OrderMaster orderMaster) {
        OrderMasterBean orderMasterBean = new OrderMasterBean();
        StoreInfo storeInfo = storeInfoMapper.selectById(orderMaster.getStoreId());
        orderMasterBean.setOrderMaster(orderMaster)
                .setStoreName(storeInfo.getStoreName())
                .setReceiverName(orderMaster.getReceiverName())
                .setReceiverGender(orderMaster.getReceiverGender())
                .setReceiverPhone(orderMaster.getReceiverPhone())
                .setReceiverAddress(orderMaster.getReceiverAddress());
        return orderMasterBean;
    }
}
